package top.yueyazhui.vhr.controller.Sys.SysBasic;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yueyazhui.vhr.bean.Response;
import top.yueyazhui.vhr.model.Department;
import top.yueyazhui.vhr.service.DepartmentService;

import java.util.List;

@RestController
@RequestMapping("/Sys/SysBasic/Department")
@Api(tags = "部门")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @GetMapping("/")
    @ApiOperation(value = "查询所有部门", notes = "以递归的方式查询所有部门")
    public List<Department> getAll() {
        return departmentService.getAll();
    }

    @PostMapping("/")
    @ApiOperation(value = "创建部门", notes = "创建部门")
    public Response insert(@RequestBody Department department) {
        departmentService.insert(department);
        if(department.getResult() == 1) {
            return Response.success("创建成功", department);
        }
        return Response.error("创建失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门", notes = "删除部门")
    public Response delete(@PathVariable Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentService.delete(department);
        if(department.getResult() == -2) {
            return Response.error("该部门下有子部门，删除失败！");
        } else if(department.getResult() == -1) {
            return Response.error("该部门下有员工，删除失败！");
        } else if(department.getResult() == 1) {
            return Response.success("删除成功！");
        }
        return Response.error("删除失败！");
    }
}
